<template>
  <div>
        <el-form :model="queryParams" ref="queryForm" :inline="true"  label-width="68px" @submit.native.prevent>
          <el-form-item label="学院名称" prop="name">
            <el-input
              v-model="queryParams.name"
              placeholder="模糊查询"
              clearable
              size="small"
              style="width: 240px"
              @keyup.enter.native="selectCollege"
            />
          </el-form-item>

          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="selectCollege">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="clickAdd"
            >新增</el-button>
          </el-col>
        </el-row>
        <el-table v-loading="loading" :data="collegeList" :header-cell-style="getRowClass">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="学院id" align="center" key="id" prop="id"  />
          <el-table-column label="学院名称" align="center" key="name" prop="name"/>
          <el-table-column label="学院代码" align="center" key="code" prop="code"/>
          <el-table-column label="院长" align="center" key="ceachName" prop="ceachName"/>
          <el-table-column
            label="操作"
            align="center"
            width="320"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope" >
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
              >修改</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <!-- 分页 -->
        <pagination
          v-show="this.sum>0"
          :total="this.sum"
          :page.sync='queryParams.page'
          :limit.sync='queryParams.number'
          @pagination="selectCollege"
        />
    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="学院名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入学院名称" maxlength="40" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="学院编号">
              <el-input v-model="form.code" placeholder="请输入学院编号" maxlength="40" />
              <!-- <el-input v-model="form.code" placeholder="请输入学院编号" maxlength="40" v-if="form.code==null"/> -->
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="院长">
              <el-select v-model="form.ceachCode"
              clearable
              size="small"
              style="width: 240px"
               placeholder="请选择">
                <el-option
                  v-for="dict in ceachCodeOptions"
                  :key="dict.id"
                  :label="dict.name"
                  :value="dict.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {getCeachCodeOptions} from "@/api/system/common"
import {selectCollegeApi,selectCollegeByCodeAndNameApi,deleteCollegeApi,updateCollegeApi,insertCollegeApi} from "@/api/system/college"
export default {
  name: "college",
  // components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      loadingCollege: true,
      refreshTable: true,
      // 导出遮罩层
      exportLoading: false,
      // 学院表格数据
      collegeList: null,
      sum: 0,
       // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 状态数据
      ceachCodeOptions:[],
      form: {},
    //   collegeId: undefined,
      // 查询参数
      queryParams: {
        page: 1,
        number: 10,
        name: undefined
      },
      // 列信息
      columns: [
        { key: 0, label: `角色编号`, visible: true },
        { key: 1, label: `角色名称`, visible: true },
        { key: 2, label: `状态`, visible: true },
        { key: 3, label: `创建时间`, visible: true }
      ],
      // 表单校验
      rules: {

      }
    };
  },
  watch: {

  },
  created() {
    getCeachCodeOptions().then(data=>{
      this.ceachCodeOptions=data.data
    })
    this.selectCollege();
  },
  methods: {
    getRowClass ({ rowIndex }) {
      if (rowIndex == 0) {
        return 'background:#afdfe4'
      } else {
        return ''
      }
    },
    //查询学院
    selectCollege(){
      selectCollegeApi(this.queryParams).then(data=>{
        this.collegeList = data.data.returnDate;
        this.sum = data.data.paginationData.sum;
        this.loading = false;
      }).catch(()=>{
        this.collegeList = null;
        this.sum = 0;
        this.loading = false;
      })
    },
    /** 重置按钮操作 */
    resetQuery() {
      if (this.$refs["queryForm"]) {
		    this.$refs["queryForm"].resetFields();
	    }
      this.selectCollege();
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm('是否确认删除学院编号为"' + row.id + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(()=> {
          deleteCollegeApi({
            "code": row.code
          }).then(data=>{
            this.selectCollege();
            this.$message({
               type: 'success',
              message: '删除成功'
            });
          })
        })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      selectCollegeApi({
        "id": row.id,
        "page":1,
        "number":1
      }).then(()=>{
        this.form.id = row.id;
        this.form.name = row.name;
        this.form.code = row.code;
        this.form.ceachCode = row.ceachCode+"";
        this.open = true;
        this.selectCollege();
      })
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        name: undefined,
        code:undefined,
        ceachCode :undefined
      };
       if (this.$refs["form"]) {
		    this.$refs["form"].resetFields();
	    }
    },
    clickAdd(){
      this.title = "添加学院"
      this.open = true;
      this.reset();
    },
     /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateCollegeApi(this.form).then(data=>{
              this.open = false;
              this.selectCollege();
              this.reset();
              this.$message({
                type: 'success',
                message: '修改成功'
              });
            })
          } else {
            insertCollegeApi(this.form).then(data=>{
              this.open = false;
              this.selectCollege();
              this.reset();
              this.$message({
                type: 'success',
                message: '添加成功'
              });
            })
          }
        }
      });
    }, // 取消按钮
    cancel() {
      this.open = false;
    }
  }
};
</script>
